<?php

namespace App\Http\Controllers;

use App\Form;
use App\Formitem;
use App\User;
use App\ShopOrder;
use Illuminate\Http\Request;
use App\Http\Requests;

use Excel;

class ExcelController extends Controller
{
    public function export(Request $request){
        $Input = $request->input();
        $cellData = [];
        if($Input["export"] === 'form'){
            $cellData = $this->getForm($request);
        }elseif($Input["export"] === 'user'){
            $cellData = $this->getUser($request);
        }elseif($Input["export"] === 'order'){
            $cellData = $this->getOrder($request);
        }
//        dd($cellData);
        Excel::create(time(),function($excel) use ($cellData){
            $excel->sheet('content', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
    }
    private function getForm($request){
        $Input = $request->input();
        $Form = Form::orderBy('id', 'desc');
        if($request->has('start')&&$request->has('end')){
            $start_date = date('d-m-Y', strtotime($Input["start"]));
            $end_date = date('d-m-Y', strtotime($Input["end"]." +1 day"));
            $str_start_date = date("Y-m-d H:i:s",strtotime($start_date));
            $str_end_date = date("Y-m-d H:i:s",strtotime($end_date));
            $Form = $Form->where('created_at','>=',"$str_start_date")->where('created_at','<=',"$str_end_date");
        }
        if($request->has('id')){
            $name = Array();
            $Form = $Form->where('parent',$Input["id"])->where('item',$Input["item"]);
            if($request->has('start')&&$request->has('end')){
                $Form = $Form->get();
            }else{
                $Form = $Form->paginate(20);
            }
            if($Input["item"]==0){
                $name = count($Form)&&$Form[0]->type==='coupon'?["联系人","联系电话","营业执照","申请状态","申请用户","申请时间"]:["联系人","联系电话","公司信息","公司简介","申请用户","申请时间"];
            }else{
                $item = Formitem::find($Input["item"]);
                $names = json_decode($item->value);
                foreach ($names as $n){
                    array_push($name,$n->name);
                }
                array_push($name,'申请用户');
                array_push($name,'申请时间');
            }
        }elseif($request->has('type')){
            if($Input["type"]=='user'){
                $name = ["联系人","联系电话","公司信息","公司简介","申请用户","申请时间"];
            }elseif($Input["type"]=='active'){
                $name = ["活动名称","城市地区","地址","人数","主办方","票价","开始时间","结束时间","活动内容","申请用户","申请时间"];
            }
            $Form = $Form->where('type',$Input["type"]);
            if($request->has('start')&&$request->has('end')){
                $Form = $Form->get();
            }else{
                $Form = $Form->paginate(20);
            }
        }
        $cellData = Array();
        array_push($cellData,$name);
        if($request->has('type')&&$Input["type"]=='active'){
            foreach ($Form as $F){
                $val = json_decode($F->form);
                $arr = [$val->name,$val->city.' '.$val->area,$val->address,$val->peoples,$val->organizers,$val->price,$val->activeTime,$val->endTime,$val->content,$F->name,$F->created_at];
                array_push($cellData,$arr);
            }
        }else{
            $coupon = false;
            if(count($Form)&&$Form[0]->type==='coupon'){
                $coupon = true;
                $couponStatus = ["待审","已通过","已拒绝"];
            }
            foreach ($Form as $F){
                $arr = json_decode($F->form);
                if($coupon){
                    array_push($arr,$couponStatus[$F->status]);
                }elseif ($Input["item"]==0&&count($arr)==2){
                    array_push($arr,'');
                    array_push($arr,'');
                }
                array_push($arr,$F->name);
                array_push($arr,$F->created_at);
                array_push($cellData,$arr);
            }
        }
        return $cellData;
    }
    private function getOrder($request){
        $input = $request->input();
        $Orders = ShopOrder::orderBy('id', 'desc');
        if($request->has("status")){
            $Orders = $Orders->where('status',$input["status"]);
        }
        if($request->has("s")){
            $Orders = $Orders->where("trade","like","%".$input["s"]."%");
        }
        if($request->has('start')&&$request->has('end')){
            $start_date = date('d-m-Y', strtotime($input["start"]));
            $end_date = date('d-m-Y', strtotime($input["end"]." +1 day"));
            $str_start_date = date("Y-m-d H:i:s",strtotime($start_date));
            $str_end_date = date("Y-m-d H:i:s",strtotime($end_date));
            $Orders = $Orders->where('created_at','>=',"$str_start_date")->where('created_at','<=',"$str_end_date")->get();
        }else{
            $Orders = $Orders->paginate(20);
        }
        $metaName = ['未','已'];
        $statusName = ['未付款','已付款','已取消'];
        $cellData = Array();
        array_push($cellData,["订单号","订单内容","价格","购买者","状态","配送","下单时间","微信支付单号","地址详情","店铺优惠","优惠券","买家留言","订单详情"]);
        foreach ($Orders as $s) {
            $user = User::find($s->user_id);
            $s->user = $user?$user->name:"0";
            $s->content = json_decode($s->content);
            $arr = [$s->trade,$s->body,$s->total,$s->user,$statusName[$s->status],$metaName[$s->meta]."配送",$s->created_at,$s->transaction,"姓名：".$s->content->address->name.'  电话：'.$s->content->address->phone.'  地址：'.$s->content->address->address.'  ',"快递费：".$s->content->shopInfo->delivery.'  优惠：'.($s->content->shopInfo->full?'满'.$s->content->shopInfo->full.'减'.$s->content->shopInfo->cut:'无')];
            if(!$s->content->coupon){
                array_push($arr,'无');
            }else{
                if($s->content->coupon->cat=='cut'){
                    array_push($arr, '满'.$s->content->coupon->content->full.'减'.$s->content->coupon->content->cut);
                }else if($s->content->coupon->cat=='cash'){
                    array_push($arr, '抵扣'.$s->content->coupon->content.'元');
                }else if($s->content->coupon->cat=='discount'){
                    array_push($arr, $s->content->coupon->content?'原价'.$s->content->coupon->content.'%':'全额抵扣');
                }
            }
            array_push($arr,isset($s->content->note)&&$s->content->note?$s->content->note:'无');
            $i = 0;
            $list = '';
            foreach($s->content->list->item as $item){
                $list = $list.$item->name.'(价格：'.$item->price.') x'.$s->content->list->num[$i].' ';
                $i++;
            }
            array_push($arr,$list);
            array_push($cellData,$arr);
        }
        return $cellData;
    }
    private function getUser($request){
        $input = $request->input();
        $Users = User::orderBy('id', 'desc');
        if($request->has("s")){
            $Users = $Users->where("name","like","%".$input["s"]."%");
        }
        if($request->has("cat")){
            $Users = $Users->where("level",$input["cat"]);
        }
        if($request->has('start')&&$request->has('end')){
            $start_date = date('d-m-Y', strtotime($input["start"]));
            $end_date = date('d-m-Y', strtotime($input["end"]." +1 day"));
            $str_start_date = date("Y-m-d H:i:s",strtotime($start_date));
            $str_end_date = date("Y-m-d H:i:s",strtotime($end_date));
            $Users = $Users->where('created_at','>=',"$str_start_date")->where('created_at','<=',"$str_end_date")->get();
        }else{
            $Users = $Users->paginate(20);
        }
        $role = array("0"=>"游客","2"=>"注册用户","3"=>"待审企业","4"=>"企业用户","5"=>"商户","8"=>"编辑","10"=>"管理员");
        $cellData = Array();
        array_push($cellData,["ID","用户名","邮箱","手机","真名","公司","角色","注册时间"]);
        foreach($Users as $user){
            $arr = [$user->id,$user->name,$user->email,$user->phone,$user->real_name,$user->company,$role[$user->level],$user->created_at];
            array_push($cellData,$arr);
        }
        return $cellData;
    }
}
